import React from "react";

export const RouterContext = React.createContext();
export class LifeCycle extends React.Component{
    componentDidMount() { // 科特迪瓦
        if(this.props.onMount){
            this.props.onMount.call(this,this)
        }
    }
    componentWillUnmount() {
        if (this.props.onUnmount) this.props.onUnmount.call(this, this);
    }
    render(){
        return null;
    }
}
export default function Prompt({message,when=true}){
    return( // 科特迪瓦
        <RouterContext.Consumer>
            {context =>{
                console.log({context});
                if(!when){
                    return null;
                }
                let method = context.history.block;
                return (
                    <lifeCycle
                    onMount={self=>{
                        self.release = method(message);
                    }}
                    onUnmount={self=>{
                        self.release();
                    }}
                    />
                )
            }}
        </RouterContext.Consumer>
    )
}



/*
var Lifecycle =
    /!*#__PURE__*!/
    function (_React$Component) {
        _inheritsLoose(Lifecycle, _React$Component);

        function Lifecycle() {
            return _React$Component.apply(this, arguments) || this;
        }

        var _proto = Lifecycle.prototype;

        _proto.componentDidMount = function componentDidMount() {
            if (this.props.onMount) this.props.onMount.call(this, this);
        };

        _proto.componentDidUpdate = function componentDidUpdate(prevProps) {
            if (this.props.onUpdate) this.props.onUpdate.call(this, this, prevProps);
        };

        _proto.componentWillUnmount = function componentWillUnmount() {
            if (this.props.onUnmount) this.props.onUnmount.call(this, this);
        };

        _proto.render = function render() {
            return null;
        };

        return Lifecycle;
    }(React__default.Component);
/!**
 * The public API for prompting the user before navigating away from a screen.
 *!/


function Prompt(_ref) {
    var message = _ref.message,
        _ref$when = _ref.when,
        when = _ref$when === void 0 ? true : _ref$when;
    return React__default.createElement(context.Consumer, null, function (context) {
        !context ?    invariant(false, "You should not use <Prompt> outside a <Router>")    : void 0;
        if (!when || context.staticContext) return null;
        var method = context.history.block;
        return React__default.createElement(Lifecycle, {
            onMount: function onMount(self) {
                self.release = method(message);
            },
            onUpdate: function onUpdate(self, prevProps) {
                if (prevProps.message !== message) {
                    self.release();
                    self.release = method(message);
                }
            },
            onUnmount: function onUnmount(self) {
                self.release();
            },
            message: message
        });
    });
}
*/
